<template>
  <div class="cart-sku" ref="target">
    <!-- 规格文本部分 -->
    <div class="attrs" @click="toggle">
      <span class="ellipsis">{{ attrsText }}</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <!-- 规格弹出层部分 -->
    <div class="layer" v-if="isShow">
      <div v-if="!goods" class="loading"></div>
     <template v-else>
          <GoodsSku @change="skuChange"  :goods="goods" :skuId='skuId'></GoodsSku>
          <XtxButton  type="primary" size="mini" style="margin-left:60px" @click="submit">确认</XtxButton>
     </template>
    </div>
  </div>
</template>

<script>
import { ref } from 'vue'
import { onClickOutside } from '@vueuse/core'
import GoodsSku from '@/views/goods/components/goods-sku.vue'
import { getSpecsAndSkus } from '@/api/product'
export default {
  components: { GoodsSku },
  name: 'CartSku',
  props: {
    attrsText: {
      type: String,
      default: ''
    },
    skuId: {
      type: String,
      default: ''
    }
  },
  setup (props, { emit }) {
    const isShow = ref(false)
    const target = ref(null)
    const goods = ref(null)

    // 隐藏
    const hide = () => {
      isShow.value = false
      // 隐藏的时候，旧的商品信息清理掉
      goods.value = null
    }
    // 显示
    const show = async () => {
      isShow.value = true
      currSkuObj.value = null
      // 每次打开，都根据 skuId 获取最新的商品信息
      const { result } = await getSpecsAndSkus(props.skuId)
      goods.value = result
    }
    // 切换
    const toggle = () => {
      isShow.value === true ? hide() : show()
    }

    onClickOutside(target, () => {
      hide()
    })

    const currSkuObj = ref(null)
    // 获取子传父改变后的sku对象( 获取子组件中选择的 skuId)
    const skuChange = (skuObj) => {
      // 当前选择规格对应的skuId
    //   console.log(skuObj)
      if (skuObj.skuId) {
        currSkuObj.value = skuObj
      }
    }

    // 用户点击确认的时候，需要把当前的 skuId 传递给父组件
    const submit = () => {
    //   console.log(currSkuId.value)
      if (currSkuObj.value && currSkuObj.value.skuId !== props.skuId) {
        hide()
        emit('change', currSkuObj.value)
      }
    }
    return { isShow, toggle, target, goods, skuChange, submit }
  }
}
</script>

<style scoped lang="less">
.cart-sku {
  height: 28px;
  border: 1px solid #f5f5f5;
  padding: 0 6px;
  position: relative;
  margin-top: 10px;
  display: inline-block;
  .attrs {
    line-height: 24px;
    display: flex;
    span {
      max-width: 230px;
      font-size: 14px;
      color: #999;
    }
    i {
      margin-left: 5px;
      font-size: 14px;
    }
  }
  .layer {
    position: absolute;
    left: -1px;
    top: 40px;
    z-index: 10;
    width: 400px;
    border: 1px solid @xtxColor;
    box-shadow: 2px 2px 4px lighten(@xtxColor, 50%);
    background: #fff;
    border-radius: 4px;
    font-size: 14px;
    padding: 20px;
    &::before {
      content: "";
      width: 12px;
      height: 12px;
      border-left: 1px solid @xtxColor;
      border-top: 1px solid @xtxColor;
      position: absolute;
      left: 12px;
      top: -8px;
      background: #fff;
      transform: scale(0.8, 1) rotate(45deg);
    }
    .loading {
      height: 224px;
      background: url(~@/assets/images/loading.gif) no-repeat center;
    }
  }
}
</style>
